'''模型层，用于存储和处理数据'''


class House:
    __house_list = []  # 此列表用于存储所有房子的信息

    def __init__(self, title, total_price, position, area, estate):
        '''创建一个对象，要对每一个对象添加id信息'''
        # self.__id = max(self.__house_list,key= lambda h:h.__id).__id+1 if self.__house_list else 1 # 方法一
        if self.__house_list:
            a_house = max(self.__house_list, key=lambda h: h.__id).__id
            self.__id = a_house + 1

        else:
            self.__id = 1
        self.__title = title
        self.__total_price = total_price
        self.__position = position
        self.__area = area
        self.__estate = estate

    @property  # 特殊属性运用 ，添加所有属性的getter 方法
    def id(self):
        return self.__id

    @property
    def title(self):
        return self.__title

    @property
    def total_price(self):
        return self.__total_price

    @property
    def position(self):
        return self.__position

    @property
    def area(self):
        return self.__area

    @property
    def estate(self):
        return self.__estate

    @property  # 添加一个虚拟属性，用作计算显示单价
    def unit_price(self):
        return self.__total_price / self.__area

    @classmethod  # 类方法，添加新房源 接口
    def add_house(cls, title, total_price, position, area, estate):
        a_house = cls(title, total_price, position, area, estate)
        cls.__house_list.append(a_house)

    @classmethod  # 类方法，获取所有房源列表信息返回 接口
    def get_house_list(cls):
        return cls.__house_list

    @classmethod  # 类方法，保存房源信息
    def save_house_info(cls, filename='house.csv'):
        try:
            with open(filename, 'w', encoding='gbk') as fw:
                # 遍历所有房子的信息，写入文件
                for a_house in cls.__house_list:
                    fw.write(str(a_house.id))
                    fw.write(',')
                    fw.write(a_house.title)
                    fw.write(',')
                    fw.write(str(a_house.total_price))
                    fw.write(',')
                    fw.write(str(a_house.position))
                    fw.write(',')
                    fw.write(str(a_house.area))
                    fw.write(',')
                    fw.write(str(a_house.estate))
                    fw.write('\r\n')


        except OSError:
            return
        return True

    @classmethod  # 类方法 ， 加载房源信息
    def load_house_info(cls, filename='house.csv'):
        try:
            with open(filename, 'r', encoding='gbk') as fr:
                # 清空列表
                cls.__house_list.clear()
                # 读取文件内的数据，形成House对象，添加到__house_list列表中显示
                for aline in fr:
                    aline = aline.strip()  # 去掉空白字符
                    infos = aline.split(',')  # 以英文的逗号拆分各个列（字段）形成字符串列表
                    id, title, total_price, position, area, estate = infos
                    id = int(id)
                    total_price = float(total_price)
                    area = float(area)
                    a_house = cls(title, total_price, position, area, estate)
                    # 修改房子的id
                    a_house.__id = id
                    cls.__house_list.append(a_house)

        except OSError:
          return False
        return True
    @classmethod
    def modify_house_total_price(cls, id, new_price):
        '''修改id对应房子的总价'''
        if new_price < 0:
            return False
        # 查找id对应的房子
        for a_house in cls.__house_list:
            if id == a_house.id:  # 找到了
                a_house.__total_price = new_price
                return True
        return False  # 没有找到对应的房子
    @classmethod
    def remove_house(cls, id):
        '''根据id删除房子'''
        for index, a_house in enumerate(cls.__house_list):
            if id == a_house.id:
                del cls.__house_list[index]
                return True
        return False